/*
 * Copyright (c) 2011  Cristian Constantin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.quizio.engine.session;

import com.quizio.engine.dao.QuizSessionDao;
import com.quizio.engine.model.Question;
import com.quizio.engine.model.QuestionUserInput;
import com.quizio.engine.model.Quiz;
import com.quizio.engine.model.QuizSession;
import com.quizio.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Cristian Constantin
 * @since 11/5/11
 */
@Service
@Transactional
public class QuizSessionService {

    @Autowired
    private QuizSessionDao sessionDao;


    /**
     * Retrieves a quiz session by its id.
     *
     * @param id the id of the quiz session
     * @return the quiz session
     */
    public QuizSession getById(Long id) {
        return sessionDao.getById(id);
    }

    /**
     * Initializes a new quiz session for the specified user
     * and the specified quiz.
     *
     * @param quiz the quiz
     * @param user the user
     * @return the newly instantiated quiz session object
     */
    public QuizSession createSession(Quiz quiz, User user) {

        QuizSession session = new QuizSession(user, quiz);

        return session;
    }

    /**
     * Initializes a quiz session. Generates the question list and
     * marks the session as active.
     *
     * @param session the quiz session to initialize
     */
    public void initSession(QuizSession session) {

        session.setQuestionsInputs(generateQuizQuestionList(session));
        session.setActive(true);
        session.setStartTime(new Date());
        session.setCurrentQuestion(1);
    }


    /**
     * Returns a list of questions that will be sent to the quiz candidate.
     * <p/>
     * Depending on the generation strategy, this list may be a subset of
     * the original list of questions, may be in a different order etc.,
     * thus offering the possibility to generate an unique set of questions
     * for each candidate.
     *
     * @param session the session to generate questions for
     * @return a list of <code>QuestionUserInput</code> objects
     */
    private List<QuestionUserInput> generateQuizQuestionList(QuizSession session) {
        List<QuestionUserInput> questions = new ArrayList<QuestionUserInput>();

        for (Question question : session.getQuiz().getQuestions()) {
            questions.add(new QuestionUserInput(question, session));
        }

        //the current implementation only shuffles the questions list
        Collections.shuffle(questions);

        return questions;
    }

}
